
def des_encrypt(s,Key):
  from pyDes import des, CBC, PAD_PKCS5
  import binascii
  """
  DES 加密
  :param s: 原始字符串
  :return: 加密后字符串,16进制
  """
  secret_key = Key  # 密码
  iv = secret_key  # 偏移
  # secret_key:加密密钥，CBC:加密模式，iv:偏移, padmode:填充
  des_obj = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
  # 返回为字节
  secret_bytes = des_obj.encrypt(s, padmode=PAD_PKCS5)
  # 返回为16进制
  return binascii.b2a_hex(secret_bytes)


def des_descrypt(s,Key):
  from pyDes import des,CBC,PAD_PKCS5
  import binascii
  """
  DES 解密
  :param s: 加密后的字符串，16进制
  :return:  解密后的字符串
  """
  secret_key = Key
  iv = secret_key
  des_obj = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
  decrypt_str = des_obj.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
  return decrypt_str

# generate the registration code according to a string
PluginID=input('要注册的插件（pluginID）=')
UserId=input('用户代码（UserId）=')

KEY='Huster63'

PlainTextStr=PluginID+'-'+KEY+'-'+UserId

# enscrypt
CipherText=des_encrypt(PlainTextStr,KEY)
print('注册码生成OK!\n===> '+str(CipherText))

#--------------------------------------------------------

# imput params
PlunginId=input("要注册的插件(PluginID) = ")  # Format : PlgId01
#RegistrationCode=input("输入您的注册码 = ")   # HEX CODE , decode result: Plg0xx-Huster63-UserID;userid depend on developer
RegistrationCode=CipherText
KEY='Huster63'

# check the validate of registration code
## get PluginId String paintext
Plaintext=des_descrypt(RegistrationCode,KEY)
if str(PlunginId) in str(Plaintext) :
  print('注册码有效，插件PluginId注册成功')
else:
  print('注册码无效')
# create a lic file
lic=open('RegistrationCode.lic',mode='w')
lic.write(str(RegistrationCode))
lic.close()